package com.work.yly.website.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.work.yly.website.service.ServiceDefine;
import com.work.yly.website.service.ServiceHelper;
import com.work.yly.website.vo.Lccp;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
public class IndexController extends BaseController {
	@RequestMapping("index")
	public String index(Model model) {
		JSONObject obj = ServiceHelper.queryLatelyLcdd(5);
		if(obj.getInt("flag")== ServiceDefine.CALL_SUCCESS){
			String tips = "%s于%s在本平台中购买了%s的%s理财产品，预计收益%.2f元。 ";
			JSONArray arr = obj.getJSONObject("data").getJSONArray("latelyLc_list");
			List<String> list = Lists.newArrayList();
			for(int i=0; i<arr.size();i++){
				JSONObject o = arr.getJSONObject(i);
				String hyxm = o.getString("hyxm");
				String sex = o.getString("sex");
				String cpmc = o.getString("cpmc");
				String gmsj = o.getString("gmsj");
				double gmje = o.getDouble("gmje");
				double yjsy = o.getDouble("yjsy");
				if(!"".equals(hyxm)){
					if("男".equals(sex)){
						hyxm = hyxm.substring(0, 1)+"先生";
					}
					else{
						hyxm = hyxm.substring(0, 1)+"女士";
					}
				}
				String sgmje;
				if(gmje > 10000){
					sgmje = String.format("%.2f万元", gmje/10000);
				}
				else{
					sgmje = String.format("%.2f元", gmje);
				}
				list.add(String.format(tips, hyxm, gmsj, sgmje, cpmc, yjsy));
			}
			model.addAttribute("notifylist",list);
			
			JSONObject ret = ServiceHelper.lccplist();
			if(ret.getInt("flag")==ServiceDefine.CALL_SUCCESS){
				JSONArray data = ret.getJSONObject("data").getJSONArray("lccp_list");
				Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
				List<Lccp> lccplist = Lists.newArrayList();
				int j=0;
				for(int i=0; i<data.size();i++){
					Lccp lccp = gson.fromJson(data.getString(i), Lccp.class);
					if("0".equals(lccp.getHdbs())&&(j<3)){
						lccplist.add(lccp);
						j++;
					}
				}
				model.addAttribute("lccp_list", lccplist);
			}

			List<Map<String, Object>> lunbo_list = Lists.newArrayList();
			ret = ServiceHelper.lccpShow();
			if(ret.getInt("flag")==ServiceDefine.CALL_SUCCESS){
				JSONArray data = ret.getJSONObject("data").getJSONArray("lccp_list");
				Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();				
				for(int i=0; i<data.size();i++){
					Lccp lccp = gson.fromJson(data.getString(i), Lccp.class);
					Map<String, Object> map = Maps.newHashMap();
					map.put("image", lccp.getCptpjs());
					map.put("url", "/finance/lc_detail?lccp_id="+lccp.getId());
					map.put("title", lccp.getCpmc());
					lunbo_list.add(map);
					if(i==2){
						break;
					}
				}
				
			}
			ret = ServiceHelper.zxxxShow();
			if(ret.getInt("flag")==ServiceDefine.CALL_SUCCESS){
				JSONArray data = ret.getJSONObject("data").getJSONArray("zxxx_list");
				for(int i=0; i<data.size();i++){
					JSONObject o = data.getJSONObject(i);
					Map<String, Object> map = Maps.newHashMap();
					map.put("image", o.getString("zxfm"));
					map.put("url", "/about/zxxx?zxxx_id="+o.getInt("id"));
					map.put("title", o.getString("zxbt"));
					lunbo_list.add(map);
					if(i==2){
						break;
					}
				}
				
			}
			model.addAttribute("lunbo_list", lunbo_list);
			
			ret = ServiceHelper.queryPageZxxx(1, 6);
			if (ret.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
				JSONArray arr1 = ret.getJSONObject("data").getJSONArray("zxxx_list");
				List<Map<String, Object>> list1 = Lists.newArrayList();
				for(int i=0; i<arr1.size(); i++){
					JSONObject o = arr1.getJSONObject(i);
					Map<String, Object> map = Maps.newHashMap();
					map.put("fbsj", o.getString("fbsj").substring(5, 10));
					map.put("zxbt", com.work.yly.website.service.StringUtils.abbr(o.getString("zxbt"),24));
					map.put("id", o.getInt("id"));
					list1.add(map);	
				}
				model.addAttribute("zxxx_list", list1);
			}
		}
		return "/index";
	}

	@RequestMapping("login")
	public String login(HttpServletRequest request, Model model) {
		String sucessUrl = request.getParameter("successUrl");
		if(StringUtils.isNotEmpty(sucessUrl)){
			model.addAttribute("sucessUrl", sucessUrl);
		}
		model.addAttribute("username", "请输入手机号");
		return "/login";
	}

	@RequestMapping("dologin")
	public String doLogin(HttpServletRequest request, Model model) {
		String userName = request.getParameter("username");
		String passWord = request.getParameter("password");
		String sucessUrl = request.getParameter("successUrl");
		
		JSONObject ret = ServiceHelper.login(userName, passWord);
		if (ret.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
			JSONObject data = ret.getJSONObject("data");
			HttpSession session = request.getSession();
			session.setAttribute("hy_id", data.getInt("hy_id"));
			session.setAttribute("zh_id", data.getInt("zh_id"));
			session.setAttribute("hyxm", data.getString("hyxm"));
			session.setAttribute("key", data.getString("key"));
			session.setAttribute("sjhm", data.getString("sjhm"));
			if(StringUtils.isNotEmpty(sucessUrl)){
				return "redirect:"+sucessUrl;
			}
			else{
				return "redirect:/index";
			}
		} else {
			if(StringUtils.isEmpty(sucessUrl)){
				model.addAttribute("sucessUrl", sucessUrl);
			}
			model.addAttribute("error", ret.getString("message"));
			model.addAttribute("username", userName);
			model.addAttribute("password", passWord);
			return "/login";
		}

	}

	@RequestMapping("restpwd")
	public String resetPwd() {
		return "/resetpwd";
	}

	@RequestMapping("register")
	public String register() {
		return "/register";
	}

	@RequestMapping("doregister")
	public String doregister(HttpServletRequest request, Model model) {
		String sjhm = request.getParameter("sjhm");
		String passWord = request.getParameter("password");
		String dxyzm = request.getParameter("dxyzm");
		String yqr_sjhm = request.getParameter("yqrhm");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("dxyzm", dxyzm);
		map.put("password", passWord);
		map.put("sjhm", sjhm);
		map.put("username", sjhm);
		map.put("yqr_sjhm", yqr_sjhm);
		JSONObject ret = ServiceHelper.register(map);
		if (ret.getInt("flag") == ServiceDefine.CALL_SUCCESS) {
			return "redirect:/index";
		} else {
			model.addAttribute("dxyzm", dxyzm);
			model.addAttribute("password", passWord);
			model.addAttribute("sjhm", sjhm);
			model.addAttribute("yqrhm", yqr_sjhm);
			model.addAttribute("error", ret.getString("message"));
			return "/register";
		}
	}

	@RequestMapping("lccplist")
	public void lccplist(HttpServletResponse response) {
		JSONObject ret = ServiceHelper.lccplist();
		renderString(response, ret.toString(), "application/json");
	}

	@RequestMapping("getyzm")
	public void getYzm(HttpServletRequest request, HttpServletResponse response){
		String sjhm = request.getParameter("sjhm");
		String yzmyt = request.getParameter("yzmyt");
		JSONObject ret = ServiceHelper.getYzm(sjhm, yzmyt);
		renderString(response, ret.toString(), "application/json");		
	}
	
}
